package com.raymond.util;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;

/**
 * desc-业务ID生成服务
 *
 * @author houmenghe
 * @date 2025/04/22 14:57
 **/
public class BusinessIdGenerateUtil {

    private static final int MAX_COUNT_PEER_SECOND = 999;
    private static final int THOUSAND = 2000;
    private static int current;
    private static int count;

    public synchronized static String generate(String type) {
        // 本工程为单体项目，目前只有一台服务器，workId置为1
        int workId = 1;
        int now = (int) (System.currentTimeMillis() / THOUSAND);

        if (now > current) {
            current = now;
            count = 0;
        }
        if (count == MAX_COUNT_PEER_SECOND) {
            count = 0;
            current++;
        }
        return String.format("%s%09d%02d%03d", type, current, workId, count++);
    }

    public static Long getSnowId() {
        Snowflake snowflake = IdUtil.getSnowflake(1, 1);
        return snowflake.nextId();
    }
}
